Log errors from watched queries - #1068
Open
giaBaoJS wants to merge 1 commit into
Open
Conversation
Errors raised while resolving or executing a watched query were only reported on the query state and to error listeners. Neither is inspected by default, so `useQuery` appeared to silently do nothing when the query was invalid. Log the error with the database's logger from `AbstractQueryProcessor`, which covers both the table resolution and query execution paths, and do the same for the `runQueryOnce` path in `useSingleQuery`. The default `onError` handler of `watchWithCallback` logged the error itself. That is now handled by the watched query, so the default handler no longer logs to avoid emitting the same error twice.
🦋 Changeset detectedLatest commit: 41fca45 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
useQuerygives no signal at all when the underlying query fails. The error is stored on the returnederrorfield and dispatched toonErrorlisteners, but nothing is inspected by default, so an invalid query looks like a query that simply never returns rows — the console stays empty, as reported in #834.The deprecated callback API does not have this problem:
watchWithCallbackdefaultsonErrorto a logger call (packages/shared-internals/src/client/BasePowerSyncDatabase.ts:659), so theWatchedQuery/useQuerypath lost that behaviour rather than never having it.Changes
Errors are now logged with the database's
logger, matching the existing'Watched query error handler threw an Error'log in the same file.AbstractQueryProcessor.updateStatelogs whenever an error is set on the state. This is the single funnel every watched-query failure passes through, which matters because the two paths are separate:linkQuery(caught byrunWithReporting) — this is what a missing table hits, sinceresolveTablesrunsEXPLAIN <sql>;onChangecallbacks ofOnChangeQueryProcessorandDifferentialQueryProcessor) — this is where runtime failures such as SQLite I/O errors surface.error: nullis used to clear a previous error, so only truthy errors are logged.useSingleQuerylogs the same way for therunQueryOncepath, which does not go through a query processor.watchWithCallback's defaultonErrorno longer logs. The watched query now does that, so keeping it would print the same error twice for every existing consumer of that API. Consumers passing their ownonErrorare unaffected, other than now also getting the log.On log noise
Queries that are expected to fail will now produce output where they previously produced none. That seemed like the right trade-off given the issue: the error is only emitted once per failure, at
errorlevel, and applications that want to handle failures themselves can supply aloggerwith a higherminLevelor their own implementation when constructing the database.Tests
Three tests in
packages/react/tests/useQuery.test.tsx(run in both normal andStrictMode), one per failure path: table resolution, query execution, andrunQueryOnce. Each asserts that the logged record carries the real underlying error, and that it reachesconsole.errorthrough the default logger — the console being empty is what the issue actually describes.All six fail on current
mainwithexpected 0 to be greater than 0.Fixes #834.